package com.kuaidian.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kuaidian.util.constant.ErrorCode;

public class JacksonMgr {
    
	
	private static ObjectMapper objectMapper = new ObjectMapper();
	public static  ObjectMapper getIns(){
		return objectMapper;
	}
	
	
	
	public static String toJSON( Object obj){
		try {
			//objectMapper.setSerializationInclusion(Include.NON_NULL);
			String string = objectMapper.writeValueAsString(obj);
			return string;
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		return "{\"code\":"+ErrorCode.ERROR+ ",\"msg\":\"to json failed!\"}";
	}
	
	public static String makeExceptionJSON(Exception exp){
		
		return "{\"code\":"+ErrorCode.SERVER_ERROR+ ",\"msg\":\"" +exp.getClass().getSimpleName()+"\"}";		

	}
	
	public static String  makeKDExceptionJSON(KDException exp){

		return "{\"code\":"+exp.getCode()+ ",\"msg\":\"" +exp.getMsg()+"\"}";		
		
	}
	
	
	
	
	
	
	private JacksonMgr(){}
}
